"""
Case Type   : 系统内部使用工具
Case Name   : 备机重建使用-q参数重建结束后数据库是否重启
Create At   : 2021/01/19
Owner       : opentestcase015
Description :
    1.指定参数-q进行备机重建
    2.查看集群状态备机状态是否为stop
    3.恢复环境
Expect      :
    1.指定参数-q进行备机重建成功
    2.查看集群状态成功，备机状态为stop
    3.成功
History     :
    Modified by lopentestcase027 2023/1/12: 优化用例步骤格式及一主两备判断方式
"""

import unittest
import os
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger

LOG = Logger()
primary_sh = CommonSH('PrimaryDbUser')


@unittest.skipIf(3 != primary_sh.get_node_num(), '非1+2环境不执行')
class LogicalReplication(unittest.TestCase):
    def setUp(self):
        LOG.info(f'--------{os.path.basename(__file__)} start--------')
        self.constant = Constant()
        self.sh_standby = CommonSH('Standby1DbUser')

    def test_system_internal_tools(self):
        LOG.info('-------step1:指定参数-q备机重建    expect: 成功--------')
        msg = self.sh_standby.build_standby(mode='-q')
        LOG.info(msg)
        self.assertIn("incremental build completed", msg)

        LOG.info('-------step2:查看备机状态   expect: 备机状态为stop-------')
        msg = self.sh_standby.execute_gsctl("query", self.constant.NOT_EXIST)
        self.assertTrue(msg)

    def tearDown(self):
        LOG.info('-------step3:恢复环境      expect:成功--------')
        restart = self.sh_standby.restart_db_cluster()
        LOG.info(restart)
        status = self.sh_standby.get_db_cluster_status(param='status')
        self.assertTrue(status)
        LOG.info(f'--------{os.path.basename(__file__)} end--------')
